<?php

require_once dirname(__FILE__) . '/../common/IController.php';
require_once dirname(__FILE__) . '/../common/BaseController.php';
require_once dirname(__FILE__) . '/UsersModel.php';
require_once dirname(__FILE__) . '/UsersView.php';

class AdminUserController extends BaseController implements IController {

	public function __construct(Database $database, FBLoginModel $loginModel, NavigationView $navigationView) {
	    parent::__construct($database, $loginModel, $navigationView);
	}

	public function doControl() {
		$html = '';

		if ($this->loginModel->getUserRole() & UsersModel::ADMIN_ROLE) {
			$um = new UsersModel($this->database);
			$uv = new UsersView($this->database);

			if ($uv->triedToDeleteUser()) {				
				$um->removeUser($uv->getUserName());

			} else if ($uv->triedToEditUser()) {
				$user = $um->getUser($uv->getUserName());
				$html .= $uv->getRegistrationForm($user, true);
			}

			if ($uv->triedToRegister()) {
				$user = $uv->getUserForUpdate();
				$um->updateUser($user);
			}

			$users = $um->getUsers();

			$html .= $uv->getUsersMarkup($users);
		} else {
			$html .= 'inte auktoriserad';
		}



		return $html;
	}
}